package com.sparrow.rpc.socket.handler;

import com.sparrow.rpc.socket.msg.req.RpcHeatBeat;
import com.sparrow.rpc.socket.msg.req.RpcLoginRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * rpc idle 心跳发送
 */
public class RpcClientHeatBeatHandler extends ChannelInboundHandlerAdapter {
    private static Logger logger = LoggerFactory.getLogger(RpcClientHeatBeatHandler.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent ise = (IdleStateEvent) evt;
            if (ise.state().equals(IdleState.WRITER_IDLE)) {
                RpcHeatBeat heatBeat = new RpcHeatBeat();
                ctx.writeAndFlush(heatBeat);
                if (logger.isDebugEnabled()) {
                    logger.debug("rpc client 发送心跳 channel:{}", ctx.channel().toString());
                }
            }
        }
    }
}
